home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / CheckboxMenuItem.java < prev    next >
Text File  |  1998-09-22  |  10KB  |  295 lines

  1. /*
  2.  * @(#)CheckboxMenuItem.java    1.31 98/08/21
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.CheckboxMenuItemPeer;
  17. import java.awt.event.*;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21.  
  22.  
  23. /**
  24.  * This class represents a check box that can be included in a menu. 
  25.  * Clicking on the check box in the menu changes its state from 
  26.  * "on" to "off" or from "off" to "on." 
  27.  * <p>
  28.  * The following picture depicts a menu which contains an instance
  29.  * of <code>CheckBoxMenuItem</code>:
  30.  * <p>
  31.  * <img src="images-awt/MenuBar-1.gif" 
  32.  * ALIGN=center HSPACE=10 VSPACE=7>
  33.  * <p>
  34.  * The item labeled <code>Check</code> shows a check box menu item 
  35.  * in its "off" state. 
  36.  * <p>
  37.  * When a check box menu item is selected, AWT sends an item event to 
  38.  * the item. Since the event is an instance of <code>ItemEvent</code>, 
  39.  * the <code>processEvent</code> method examines the event and passes 
  40.  * it along to <code>processItemEvent</code>. The latter method redirects 
  41.  * the event to any <code>ItemListener</code> objects that have
  42.  * registered an interest in item events generated by this menu item. 
  43.  *
  44.  * @version 1.31, 08/21/98
  45.  * @author     Sami Shaio
  46.  * @see         java.awt.event.ItemEvent
  47.  * @see         java.awt.event.ItemListener
  48.  * @since       JDK1.0
  49.  */
  50. public class CheckboxMenuItem extends MenuItem implements ItemSelectable {
  51.  
  52.     boolean state = false;
  53.  
  54.     transient ItemListener itemListener;
  55.  
  56.     private static final String base = "chkmenuitem";
  57.     private static int nameCounter = 0;
  58.  
  59.     /*
  60.      * JDK 1.1 serialVersionUID 
  61.      */
  62.      private static final long serialVersionUID = 6190621106981774043L;
  63.  
  64.     /**
  65.      * Create a check box menu item with an empty label.
  66.      * The item's state is initially set to "off." 
  67.      * @since   JDK1.1
  68.      */
  69.     public CheckboxMenuItem() {
  70.     this("", false);
  71.     }
  72.  
  73.     /**
  74.      * Create a check box menu item with the specified label. 
  75.      * The item's state is initially set to "off." 
  76.  
  77.      * @param     label   a string label for the check box menu item, 
  78.      *                or <code>null</code> for an unlabeled menu item.
  79.      * @since     JDK1.0
  80.      */
  81.     public CheckboxMenuItem(String label) {
  82.     this(label, false);
  83.     }
  84.  
  85.     /**
  86.      * Create a check box menu item with the specified label and state.
  87.      * @param      label   the button label.
  88.      * @param      state   the initial state of the menu item, where
  89.      *                     <code>true</code> indicates "on" and 
  90.      *                     <code>false</code> indicates "off."
  91.      * @since      JDK1.1
  92.      */
  93.     public CheckboxMenuItem(String label, boolean state) {
  94.         super(label);
  95.         this.state = state;
  96.     }
  97.  
  98.     /**
  99.      * Construct a name for this MenuComponent.  Called by getName() when
  100.      * the name is null.
  101.      */
  102.     String constructComponentName() {
  103.     return base + nameCounter++;
  104.     }
  105.  
  106.     /**
  107.      * Creates the peer of the checkbox item.  This peer allows us to
  108.      * change the look of the checkbox item without changing its 
  109.      * functionality.
  110.      * Most applications do not call this method directly. 
  111.      * @see     java.awt.Toolkit#createCheckboxMenuItem(java.awt.CheckboxMenuItem)
  112.      * @see     java.awt.Component#getToolkit()
  113.      * @since   JDK1.0
  114.      */
  115.     public void addNotify() {
  116.         synchronized (getTreeLock()) {
  117.         if (peer == null)
  118.             peer = Toolkit.getDefaultToolkit().createCheckboxMenuItem(this);
  119.         super.addNotify();
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * Determines whether the state of this check box menu item 
  125.      * is "on" or "off." 
  126.      * @return      the state of this check box menu item, where
  127.      *                     <code>true</code> indicates "on" and 
  128.      *                     <code>false</code> indicates "off."
  129.      * @see        java.awt.CheckboxMenuItem#setState
  130.      * @since      JDK1.0
  131.      */
  132.     public boolean getState() {
  133.     return state;
  134.     }
  135.  
  136.     /**
  137.      * Sets this check box menu item to the specifed state.
  138.      * The boolean value <code>true</code> indicates "on" while 
  139.      * <code>false</code> indicates "off." 
  140.      * @param      b   the boolean state of this 
  141.      *                      check box menu item.
  142.      * @see        java.awt.CheckboxMenuItem#getState
  143.      * @since      JDK1.0
  144.      */
  145.     public synchronized void setState(boolean b) {
  146.     state = b;
  147.     CheckboxMenuItemPeer peer = (CheckboxMenuItemPeer)this.peer;
  148.     if (peer != null) {
  149.         peer.setState(b);
  150.     }
  151.     }
  152.  
  153.     /**
  154.      * Returns the an array (length 1) containing the checkbox menu item
  155.      * label or null if the checkbox is not selected.
  156.      * @see ItemSelectable
  157.      */
  158.     public synchronized Object[] getSelectedObjects() {
  159.         if (state) {
  160.             Object[] items = new Object[1];
  161.             items[0] = label;
  162.             return items;
  163.         }
  164.         return null;
  165.     }
  166.  
  167.     /**
  168.      * Adds the specified item listener to receive item events from
  169.      * this check box menu item.
  170.      * @param         l the item listener.
  171.      * @see           java.awt.event.ItemEvent
  172.      * @see           java.awt.event.ItemListener
  173.      * @see           java.awt.Choice#removeItemListener
  174.      * @since         JDK1.1
  175.      */ 
  176.     public synchronized void addItemListener(ItemListener l) {
  177.         itemListener = AWTEventMulticaster.add(itemListener, l);
  178.         newEventsOnly = true;
  179.     }
  180.  
  181.     /**
  182.      * Removes the specified item listener so that it no longer receives
  183.      * item events from this check box menu item.
  184.      * @param         l the item listener.
  185.      * @see           java.awt.event.ItemEvent
  186.      * @see           java.awt.event.ItemListener
  187.      * @see           java.awt.Choice#addItemListener
  188.      * @since         JDK1.1
  189.      */ 
  190.     public synchronized void removeItemListener(ItemListener l) {
  191.         itemListener = AWTEventMulticaster.remove(itemListener, l);
  192.     }
  193.  
  194.     // REMIND: remove when filtering is done at lower level
  195.     boolean eventEnabled(AWTEvent e) {
  196.         if (e.id == ItemEvent.ITEM_STATE_CHANGED) {
  197.             if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 ||
  198.                 itemListener != null) {
  199.                 return true;
  200.             } 
  201.             return false;
  202.         }
  203.         return super.eventEnabled(e);
  204.     }        
  205.  
  206.     /**
  207.      * Processes events on this check box menu item. 
  208.      * If the event is an instance of <code>ItemEvent</code>, 
  209.      * this method invokes the <code>processItemEvent</code> method.
  210.      * If the event is not an item event,
  211.      * it invokes <code>processEvent</code> on the superclass.
  212.      * <p>
  213.      * Check box menu items currently support only item events.
  214.      * @param        e the event
  215.      * @see          java.awt.event.ItemEvent
  216.      * @see          java.awt.CheckboxMenuItem#processItemEvent
  217.      * @since        JDK1.1
  218.      */
  219.     protected void processEvent(AWTEvent e) {
  220.         if (e instanceof ItemEvent) {
  221.             processItemEvent((ItemEvent)e);
  222.         return;
  223.         }
  224.     super.processEvent(e);
  225.     }
  226.  
  227.     /** 
  228.      * Processes item events occurring on this check box menu item by
  229.      * dispatching them to any registered <code>ItemListener</code> objects. 
  230.      * <p>
  231.      * This method is not called unless item events are 
  232.      * enabled for this menu item. Item events are enabled 
  233.      * when one of the following occurs:
  234.      * <p><ul>
  235.      * <li>An <code>ItemListener</code> object is registered 
  236.      * via <code>addItemListener</code>.
  237.      * <li>Item events are enabled via <code>enableEvents</code>.
  238.      * </ul>
  239.      * @param       e the item event.
  240.      * @see         java.awt.event.ItemEvent
  241.      * @see         java.awt.event.ItemListener
  242.      * @see         java.awt.CheckboxMenuItem#addItemListener
  243.      * @see         java.awt.MenuItem#enableEvents
  244.      * @since       JDK1.1
  245.      */  
  246.     protected void processItemEvent(ItemEvent e) {
  247.         if (itemListener != null) {
  248.             itemListener.itemStateChanged(e);
  249.         }
  250.     }
  251.  
  252.     /**
  253.      * Returns the parameter string representing the state of this check 
  254.      * box menu item. This string is useful for debugging. 
  255.      * @return     the parameter string of this check box menu item.
  256.      * @since      JDK1.0
  257.      */
  258.     public String paramString() {
  259.     return super.paramString() + ",state=" + state;
  260.     }
  261.  
  262.     /* Serialization support. 
  263.      */
  264.  
  265.     private int checkboxMenuItemSerializedDataVersion = 1;
  266.  
  267.  
  268.     private void writeObject(ObjectOutputStream s)
  269.       throws java.io.IOException 
  270.     {
  271.       s.defaultWriteObject();
  272.  
  273.       AWTEventMulticaster.save(s, itemListenerK, itemListener);
  274.       s.writeObject(null);
  275.     }
  276.  
  277.  
  278.     private void readObject(ObjectInputStream s)
  279.       throws ClassNotFoundException, IOException 
  280.     {
  281.       s.defaultReadObject();
  282.  
  283.       Object keyOrNull;
  284.       while(null != (keyOrNull = s.readObject())) {
  285.     String key = ((String)keyOrNull).intern();
  286.  
  287.     if (itemListenerK == key) 
  288.       addItemListener((ItemListener)(s.readObject()));
  289.  
  290.     else // skip value for unrecognized key
  291.       s.readObject();
  292.       }
  293.     }
  294. }
  295.